////
//
/// \file   ./src/if_statement_node.cpp
//
//  Copyright 2007, 2008 Chandler Carruth
//
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file except in compliance with the License.  You may obtain a copy
//  of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
//  License for the specific language governing permissions and limitations
//  under the License.
//
////

#include <inc/parser/if_statement_node.hpp>

#include <boost/scoped_ptr.hpp>

#include <inc/parser/expression_node.hpp>
#include <inc/parser/statement_node.hpp>
#include <inc/parser/ws_parser.hpp>

namespace inc {
namespace parser {

using std::string;
using boost::scoped_ptr;

/// \brief  Basic constructor for if-statement nodes
/// \author Chandler Carruth
/// \date   2008.06.08

IfStatementNode::IfStatementNode()
    : ws(new WSParser)
{}

/// \brief  Empty destructor, all members are smart pointers
/// \author Chandler Carruth
/// \date   2008.06.08

IfStatementNode::~IfStatementNode() {}

/// \brief  Parse a single if-statement in INC
/// \author Chandler Carruth
/// \date   2007.07.26
///
/// This parses a single statement to be conditionally executed. It cannot
/// nest, or contain any block-like structures.

bool
IfStatementNode::parse( const iterator &first,
                        const iterator &last,
                        iterator &last_parsed )
{
  assert( get_text().empty() && !expr && !stmt
          && "This node was parsed more than once!" );

  // All if-statements begin with the "if" keyword.
  if( std::distance( first, last ) >= 2
      && string( first, first + 2 ) == "if" )
  {
    iterator current = first + 2;
    if( ws->parse_ws( current, last, last_parsed ) )
    {
      current = last_parsed;

      boost::scoped_ptr<ExpressionNode> tmp_expr( new ExpressionNode );
      if( tmp_expr->parse( current, last, last_parsed ) )
      {
        current = last_parsed;
        if( ws->parse_ws( current, last, last_parsed ) )
        {
          current = last_parsed;
          if( std::distance( current, last ) >= 4
              && string( current, current + 4 ) == "then" )
          {
            current += 4;
            if( ws->parse_ws( current, last, last_parsed ) )
            {
              current = last_parsed;

              boost::scoped_ptr<StatementNode> tmp_stmt( new StatementNode );
              if( tmp_stmt->parse( current, last, last_parsed ) )
              {
                set_text( string( first, last_parsed ) );
                expr.swap( tmp_expr );
                stmt.swap( tmp_stmt );
                return true;
              }
            }
          }
        }
      }
    }
  }

  //  No valid parse found.
  last_parsed = first;
  return false;
}

/// \brief  Convenience getter for the expression sub-node
/// \author Chandler Carruth
/// \date   2008.05.16

ExpressionNode* IfStatementNode::get_expression() const
{
  return expr.get();
}

/// \brief  Convenience getter for the statement sub-node
/// \author Chandler Carruth
/// \date   2008.05.16

StatementNode* IfStatementNode::get_statement() const
{
  return stmt.get();
}

} //  end parser namespace
} //  end inc namespace
